// index/index.js

const request = require('../util/request')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        timer: 0, //控制定时器
        curNews: 0, //获取当前新闻
        swiperList: [], //顶部轮播图数组
        recommend: [],
        allRecommend: [],
        categoryList: [],
        newsList: [],
        pageIndex: 1, //默认为第一页
        pageSize: 1, //每一页有两条数据
        items: 'transform: translate(0rpx,0rpx)',
        pageNum: 1,
        total: 0,
        loading: 'loading...',
    },


    //1.获取顶部轮播图的图片
    getswiperList() {
        return new Promise((resolve, reject) => {
            request.get('/wx/index', {
                type: 'banner'
            }).then((res) => {
                this.setData({
                    swiperList: res
                })

            })
            resolve()
        })

    },


    //2.获取分类栏数据
    getCategoryList() {
        return new Promise((resolve, reject) => {
            request.get('/wx/index', {
                type: 'category'
            }).then((res) => {
                this.setData({
                    categoryList: res
                })
            })
            resolve()
        })
    },

    //3.获取新闻推荐内容并触发新闻轮播
    getNews() {
        return new Promise((resolve, reject) => {
            request.get('/wx/index', {
                type: 'news'
            }).then((res) => {
                this.setData({
                    newsList: res
                })
                //获取完新闻数据后获取新闻轮播
                this.curNews() //新闻轮播
                //完成轮播获取后执行传入的函数
            })
            resolve()
        })
    },

    //新闻轮播方法
    curNews() {
        //如果timer有值的话就clear并重新赋值
        if (this.data.timer) {
            clearInterval(this.data.timer);
            this.data.timer = 0;
        }
        //给timer赋值
        this.data.timer = setInterval(() => {
            //获取当前新闻
            let curNews = this.data.curNews;
            curNews++
            if (curNews == this.data.newsList.length) {
                setTimeout(() => {
                    this.setData({
                        curNews: 0,
                        items: 'transform: translate(0rpx,rpx);'
                    })
                }, 200)
            }
            this.setData({
                curNews,
                items: 'transform: translate(0rpx,-' + curNews * 60 + 'rpx); transition:transform 0.3s'
            })
        }, 3000)
    },

    //4.获取推荐部分图片
    getRecommend() {
        return new Promise((resolve, reject) => {
            request.get('/wx/getRecommend', {
                pageNum: this.data.pageNum,
                pageSize: this.data.pageSize
            }).then((res) => {
                this.setData({
                    total: res.total,
                    recommend: [...this.data.recommend, ...res.rows]
                })
            })
            resolve()
        })
    },

    refreshPageNum() {
        this.setData({
            pageNum: 1,
            recommend: [],
            loading: 'lodaing...'
        })
    },



    //下拉刷新
    onPullDownRefresh: function () {
        Promise.all([this.getswiperList(), this.getCategoryList(), this.getNews(), this.refreshPageNum(), this.getRecommend()]).then((res) => {
            wx.stopPullDownRefresh({
                success(res) {
                    console.log('刷新成功');
                }
            });
        }).catch(e => {
            console.log('加载失败');
        })

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        Promise.all([this.getswiperList(), this.getCategoryList(), this.getNews(), this.refreshPageNum(), this.getRecommend()]).then((res) => {
            console.log('所有数据加载完成');
        }).catch(e => {
            console.log('加载失败')
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    // onPullDownRefresh: function () {

    // },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        //先判断数组长度是否小于total
        if (this.data.recommend.length < this.data.total) {
            this.data.pageNum++;
            this.getRecommend()
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})